package chap02.HighArray;

/**
 * Created by root on 2017/4/26.
 */
public class HighArray {
    private long[] a;
    private int nElems;

    public HighArray(int max){
        a = new long[max];
        nElems = 0;
    }

    public int getSize(){
        return nElems;
    }

    public void display(){
        for (int j=0; j<nElems; j++) {
            System.out.print(a[j] + " ");
        }
        System.out.println();
    }

    public void insert(long value){
        a[nElems] = value;
        nElems++;
    }

    public boolean search(long searchKey){
        int j;
        for (j=0; j<nElems; j++) {
            if (a[j] == searchKey) {
                break;
            }
        }
        if (j == nElems) {
            return false;
        } else {
            return true;
        }
    }

    public boolean delete(long searchKey){
        int j;
        for (j=0; j<nElems; j++) {
            if (a[j] == searchKey) {
                break;
            }
        }
        if (j == nElems) {
            return false;
        } else {
            for (int k=j; k<nElems-1; k++) {
                a[k] = a[k+1];
            }
            nElems--;
            return true;
        }
    }

    public long getMax(){
        if (a == null) {
            return -1;
        }
        long temp = 0;
        int i;
        for (i = 0; i < nElems; i++) {
            if (temp < a[i]) {
                temp = a[i];
            }
        }
        return temp;
    }

    public long removeMax(){
        if (a == null) {
            return -1;
        }
        long temp = 0;
        int i;
        int max = 0;
        for (i = 0; i < nElems; i++) {
            if (temp < a[i]) {
                temp = a[i];
                max = i;
            }
        }
        for (int k=max; k<(nElems-1); k++) {
            a[k] = a[k+1];
        }
        nElems--;
        return temp;
    }

}
